﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace Traviesuras.Models
{
    public class clsConfiguracion
    {
        public static bool enviar_mail(string mailto,string asunto) 
        {
            try
            {
                clsUsuario clsUser = new clsUsuario();
                clsUser.clsPersona = new clsPersona();
                clsUser.clsPersona.email = mailto;
                MailMessage email = new MailMessage();
                email.To.Add(new MailAddress(mailto));
                email.From = new MailAddress("cristy@parquetraviesuras.com");
                email.Subject = asunto;
                clsAccesoDB.RecuperarLogin(ref clsUser);
                email.Body = "Recuperacion de Contrasena "+clsUser.clsPersona.nombres_completos+"\n Usuario: "+clsUser.nombre_usuario+"\n"+"Contrasena: "+clsUser.contrasenaString+
                    "\n\n\nSi Resulta muy Dificil recordar su Contrasena Cambiela en el Sistema Gracias."+
                    "\n\n\n Parque Traviesuras";
                email.Priority = MailPriority.Normal;

                SmtpClient smtp = new SmtpClient("mx1.hostinger.es", 2525);
                smtp.EnableSsl = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("cristy@parquetraviesuras.com", "nosenose36");

                string output = null;
                clsUser = new clsUsuario();

                try
                {
                    smtp.Send(email);
                    email.Dispose();
                    output = "Corre electrónico fue enviado satisfactoriamente.";
                }
                catch (Exception)
                {
                    output = "Error enviando correo electrónico: ";
                }

                Console.WriteLine(output);



                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static string JsonString<T>(List<T> list, bool combo = false)
        {
            try
            {
                //JObject JSonObj = cls.js.Value<object>("callback");

                JObject JSonObj = JObject.FromObject(new { callback = list });
                //object js = JSonObj.Value<object>("callback");
                //string sde = js.ToString();

                string sde = JSonObj.ToString(Newtonsoft.Json.Formatting.None);
                string json = "";
                foreach (var item in sde)
                {
                    if (item == '"')
                    {
                        json = json + "";
                        if (combo)
                            json = json + "'";
                    }
                    else
                    {
                        json = json + item;
                    }
                }
                if (combo)
                {
                    json = json.Replace("{'callback':", "");
                    json = json.Replace("]}", "]");
                }
                else
                {
                    json = json.Replace("{callback:", "");
                    json = json.Replace("]}", "]");
                }
                return json;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static ActionResult GetFile()
        {
            string name = "//log.txt"; ;
            
            FileInfo info = new FileInfo(name);
            if (!info.Exists)
            {
                using (StreamWriter writer = info.CreateText())
                {
                    writer.WriteLine("Hello, I am a new text file");

                }
            }
            //return File(name, "text/plain");
            return null;
        }
    }
}